package com.hzncc.flowable_diboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.entity.IamUser;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.dto.CustomConnectorDTO;
import com.hzncc.flowable_diboot.entity.StarConnector;
import com.hzncc.flowable_diboot.mapper.StarConnectorMapper;
import com.hzncc.flowable_diboot.service.CustomConnectorService;
import com.hzncc.flowable_diboot.vo.CustomConnectorListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.CustomConnector;
import com.hzncc.flowable_diboot.mapper.CustomConnectorMapper;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 客户联系人 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-15
 * Copyright © MyCorp
 */
@Slf4j
@Service
public class CustomConnectorServiceImp extends BaseServiceImpl<CustomConnectorMapper, CustomConnector> implements CustomConnectorService {

    private final StarConnectorMapper starConnectorMapper;

    public CustomConnectorServiceImp(StarConnectorMapper starConnectorMapper) {
        this.starConnectorMapper = starConnectorMapper;
    }

    /**
     * 客户联系人查询
     *
     * @param queryDto   查询条件
     * @param pagination 分页信息
     * @param type       查询分类 self - 我的客户 star - 关注客户 resp - 负责的客户  sub - 下属客户
     * @return
     */
    @Override
    public JsonResult<List<CustomConnectorListVO>> selectCustomConnector(CustomConnectorDTO queryDto, Pagination pagination, String type) {
        QueryWrapper<CustomConnectorDTO> queryWrapper = QueryBuilder.toQueryWrapper(queryDto);
        if (pagination != null && V.isEmpty(pagination.getOrderBy())) {
            pagination.setOrderBy(Pagination.ORDER_BY_ID_DESC);
        }
        // 当前登录用户
        IamUser currentUser = IamSecurityUtils.getCurrentUser();
        // 获取我的客户联系人
        if ("self".equals(type)) {
            queryWrapper.and(wrapper -> wrapper.eq("user_id", currentUser.getId())
                    .or()
                    .eq("create_by", currentUser.getId()));
        }
        if ("resp".equals(type)) {
            queryWrapper.eq("user_id", currentUser.getId());
        }
        if ("sub".equals(type)) {
            // todo 下属客户联系人查询 需确定组织架构
        }
        // 获取我关注的客户联系人
        if ("star".equals(type)) {
            List<CustomConnectorListVO> customerListVOS = starConnectorMapper.selectStarConnector(queryDto.getConnectWay(), queryDto.getCustomerId(), currentUser.getId());
            return JsonResult.OK(customerListVOS);
        }
        List<CustomConnectorListVO> viewObjectList = getViewObjectList(queryWrapper, pagination, CustomConnectorListVO.class);
        // 查询用户是否关注客户联系人
        if (!viewObjectList.isEmpty()) {
            QueryWrapper<StarConnector> qw = new QueryWrapper();
            qw.eq("user_id", currentUser.getId());
            qw.in("connector_id", viewObjectList.stream().map(CustomConnectorListVO::getId).toArray());
            List<StarConnector> starConnectors = starConnectorMapper.selectList(qw);
            Set<String> connectorIds = starConnectors.stream().map(StarConnector::getConnectorId).collect(Collectors.toSet());
            viewObjectList.forEach(e -> {
                e.setIsStar(connectorIds.contains(e.getId()));
            });
        }
        return JsonResult.OK(viewObjectList);
    }

    /**
     * 变更负责人
     *
     * @param userId 新负责人
     * @param id
     */
    @Override
    public void change(String userId, String id) {
        CustomConnector entity = this.getEntity(id);
        entity.setUserId(userId);
        this.updateById(entity);
    }
}